package com.jiuan.transfer.mqtt.domain;
/*
 * Created on 28-Mar-2006
 * Created by Paul Gardner
 * Copyright (C) 2006 Aelitis, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * AELITIS, SAS au capital de 46,603.30 euros
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 *
 */



import java.security.SecureRandom;
import java.util.Random;

public class UUIDGenerator {
    private static Random random = new SecureRandom();

    /**
     * 128 bit UUID using random method
     *
     * @returnd
     */

    public static synchronized byte[] generateUUID(int length) {
        byte[] bytes = new byte[length];

        random.nextBytes(bytes);

        return (bytes);
    }

    public static String generateUUIDString(int length) {
        byte[] bytes = generateUUID((length+1)/2);

        String res = ByteFormatter.encodeString(bytes).toLowerCase();

        //return (res.substring(isOdd(length)?1:0));
        return (res.substring(length &1));
    }


    @SuppressWarnings("unused")
    private static boolean isOdd(int num) {

        return ((num &1) !=0);
    }

    public static void main(String[] args) {
        System.out.println(generateUUIDString(4));
        System.out.println(5 &1);
    }
}
